package root.electronclass.electronclass.school

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.BindingHolder
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.common.util.HeadUtil
import root.electronclass.electronclass.mvp.GlobalParameter
import root.electronclass.electronclass.mvp.base.BaseResponse
import root.electronclass.electronclass.mvp.base.CallbackNext
import root.electronclass.electronclass.mvp.entity.*
import root.electronclass.electronclass.mvp.rest.RestManager
import root.electronclass.electronclass.databinding.*
import root.electronclass.electronclass.other.PictureActivity
import root.electronclass.electronclass.school.model.SchoolNotificationModel
import retrofit2.Call
import root.electronclass.electronclass.ext.startFragment

class SchoolFragment2 : ViewBindingFragment<FragmentSchoolBinding>() {
    private val viewModel: SchoolNotificationModel by viewModels()
    private val schoolInfoVO: OrgSchoolInfoVO = GlobalParameter.getSchoolInfo()
    private var pro = 0
    private var picList = arrayListOf<String>()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.schoolSystem.text = schoolInfoVO.schoolType
        binding.schoolNature.text = schoolInfoVO.schoolNature
        binding.schoolPersonNum.text = schoolInfoVO.studentCount.toString()
        binding.schoolTeacherNum.text = schoolInfoVO.teacherCount.toString()
        showWaitingMessage()
        initSchoolInfoRecyclerView()
        initSchoolLeaderRecyclerView()
        initSchoolStyleRecyclerView()
        initSchoolDirectorRecyclerView()
        binding.tvLeaderMore.setOnClickListener {
            startActivity(Intent(requireActivity(),SchoolHonorActivity::class.java))
        }
        binding.tvDirectorMore.setOnClickListener {
            startFragment(SchoolDirectorFragment::class.java)
        }
        binding.tvStyleMore.setOnClickListener {
            startActivity(Intent(requireActivity(),SchoolStyleActivity::class.java))
        }
    }

    private fun getSchoolInfo() {
        RestManager.getInstance().initRest().schoolInfo().enqueue(object : CallbackNext<BaseResponse<OrgSchoolInfoVO>>(mDialogViewModel) {

            override fun onSuccessful(
                call: Call<BaseResponse<OrgSchoolInfoVO>>, response: BaseResponse<OrgSchoolInfoVO>
            ) {
                if (response.isSuccess) {
                    GlobalParameter.setSchoolInfo(response.rows)
                }
            }

            override fun onError(
                call: Call<BaseResponse<OrgSchoolInfoVO>>, response: BaseResponse<OrgSchoolInfoVO>
            ) {

            }
        })
    }


    private fun initSchoolInfoRecyclerView() {
        val list = arrayListOf(SchoolInfoData("校训", schoolInfoVO.motto), SchoolInfoData("学校简介", schoolInfoVO.introduction),
            SchoolInfoData("办学条件", schoolInfoVO.bxtj))

        val adapter = object : BasicAdapter<SchoolInfoData, ItemSchoolInfoBinding>() {
            override fun onBindViewHolder(holder: BindingHolder<ItemSchoolInfoBinding>, item: SchoolInfoData?) {
                holder.binding.type.text = item?.title
                holder.binding.content.text = item?.content
            }
        }
        adapter.refresh(list)
        binding.schoolInfoRecycler.adapter = adapter

    }

    private fun initSchoolLeaderRecyclerView() {
        binding.leaderRecycler.layoutManager = LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false)
        binding.leaderRecycler.adapter = adapter
        viewModel.rcHonor.observe(requireActivity()) {
            adapter.replaceAll(it)
            dismissProgress(pro + 50)
        }
        viewModel.rcHonorList()
    }

    private val adapter = object : BasicAdapter<RcHonor, ItemSchoolInfoTeacherBinding>() {
        override fun onBindViewHolder(holder: BindingHolder<ItemSchoolInfoTeacherBinding>, item: RcHonor?) {
            holder.binding.pic.setImageURI(item?.imgUrlFormat?.get(0)?.url)
            holder.binding.name.text = item?.name
        }
    }
    private fun initSchoolStyleRecyclerView() {

        val adapter = object : BasicAdapter<SchoolPicDto, ItemSchoolStylePicBinding>() {
            override fun onBindViewHolder(holder: BindingHolder<ItemSchoolStylePicBinding>, item: SchoolPicDto?) {
                holder.binding.picName.text = item?.name
                holder.binding.picSw.setImageURI(item?.url)
                holder.binding.item.setOnClickListener {
                    PictureActivity.startActivity(activity!!, picList, holder.adapterPosition)
                }
            }
        }
//        Log.i("SchoolInfoFragment", "initSchoolStyleRecyclerView: " + schoolInfoVO.picJson.toString())
        if (schoolInfoVO.picJsonFormat != null && schoolInfoVO.picJsonFormat.size > 0) {
            for (pic in schoolInfoVO.picJsonFormat) {
                picList.add(pic.url)
            }
        }
        binding.schoolStyleRecycler.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
        binding.schoolStyleRecycler.adapter = adapter
        if (schoolInfoVO.picJsonFormat != null && schoolInfoVO.picJsonFormat.size > 0) {
            adapter.refresh(schoolInfoVO.picJsonFormat)
        }

    }

    private fun initSchoolDirectorRecyclerView() {
        val dto = TeacherDto()
        dto.departId = schoolInfoVO.deptId
        dto.positions = intArrayOf(30)
        dto.isRealName = 1
        RestManager.getInstance().initRest().teacherInfo(dto).enqueue(object : CallbackNext<BaseResponse<List<UserInfoTeacherVO>>>(mDialogViewModel) {
            override fun onSuccessful(
                call: Call<BaseResponse<List<UserInfoTeacherVO>>>, response: BaseResponse<List<UserInfoTeacherVO>>
            ) {
                if (response.isSuccess) {
                    binding.tvDirectorUnm.text = "合计："+response.total.toString()+"人"
                    val adapter = object : BasicAdapter<UserInfoTeacherVO, ItemSchoolDirectorBinding>() {
                        override fun onBindViewHolder(
                            holder: BindingHolder<ItemSchoolDirectorBinding>, item: UserInfoTeacherVO?
                        ) {
                            if (item?.userInfoVo?.facePhotoFormat.orEmpty().isEmpty()) {
                                holder.binding.headIv.setActualImageResource(HeadUtil.getHeadPic(if (item?.gender == 1) 1 else 0))
                            } else {
                                holder.binding.headIv.setImageURI(item?.userInfoVo?.facePhotoFormat?.get(0)?.url)
                            }
                            holder.binding.name.text = item?.name

                        }
                    }
                    binding.directorRecycler.adapter = adapter
                    binding.directorRecycler.layoutManager = GridLayoutManager(activity, 4)
                    adapter.refresh(response.rows)
                    dismissProgress(pro + 50)
                }
            }

            override fun onError(
                call: Call<BaseResponse<List<UserInfoTeacherVO>>>, response: BaseResponse<List<UserInfoTeacherVO>>
            ) {
                dismissProgress()
            }
        })


    }

}